package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

import gov.nhso.prov.acc.beans.Acc6TransactionBean;
import gov.nhso.prov.acc.beans.Acc6VenderBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

@SuppressWarnings("rawtypes")
public class ACC60403 extends Utility{
	public String url = "/apps/acc6/ACC60403A.jsp";
	public ACC60403(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if(job.equals("init")){
			this.init(request);
		}else if (job.equals("search")) {
			this.search(request);
		}
	}
	
	private String init (HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			request.setAttribute("lsBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("job", "beforeSearch");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60403A.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			List ls = new ArrayList();
			List lsVender = new ArrayList();
			String provinceId = this.getAttr(request, "provinceId");
			String budgetYear = this.getAttr(request, "budgetYear");
			String allocateYear = this.getAttr(request, "allocateYear");
			String mm = this.getAttr(request, "mm");
			String condition = "";
			int iMonth = Integer.parseInt(mm);
			int iYear = Integer.parseInt(budgetYear) - 543;
			if (iMonth >= 10){
				iYear = iYear - 1;
			}
			String allocateCode = this.getAttr(request, "allocateCode");
			Acc6TransactionBean atb = new Acc6TransactionBean();
			Acc6VenderBean avb = new Acc6VenderBean();
			atb.setProvinceId(provinceId);
			if (allocateCode != null && !allocateCode.equals("")) {
				atb.setAllocateCode(allocateCode);
			} else {
				condition = " AND ALLOCATE_CODE LIKE '"+allocateYear.substring(2, 4)+"%' ";
			}
			atb.setTransactionType("C");
			ls = dao.selectList(atb, " AND TO_CHAR(CHEQUE_DATE,'YYYY-MM') = '"+iYear+"-"+mm+"' " + condition);
			avb.setProvinceId(provinceId);
			avb.setVenderType("C");
			lsVender = dao.selectList(avb);
			
			request.setAttribute("listData", ls);
			request.setAttribute("listAllocate", this.dataUtil(dao).lsAllocateByBudgetYear(allocateYear));
			request.setAttribute("lsBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("listVender", lsVender);
			request.setAttribute("provinceId", provinceId);
			request.setAttribute("budgetYear", budgetYear);
			request.setAttribute("allocateYear", allocateYear);
			request.setAttribute("mm", this.getAttr(request, "mm"));
			request.setAttribute("allocateCode", allocateCode);
			if (ls.size() == 0) {
				this.message(request).setMsgCode("I001");
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60403A.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
